<script lang="ts" setup>
import store from '@/store';
import { useRoute } from 'vue-router'
import { watchEffect, ref } from 'vue'
import { storeToRefs } from 'pinia'
import GoodsImage from './components/goods-image.vue'
import GoodsName from './components/goods-name.vue';
import goodsSales from './components/goods-sales.vue';
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'

const { goods } = store()
const route = useRoute()
watchEffect(() => {
  if (route.fullPath === `/goods/${route.params.id}`) {
    // 切换页面时重置数据
    goods.delGoodsInfo()
    // 请求数据
    goods.getgoodsInfo(route.params.id as string)
  }
})
const { goodsInfo } = storeToRefs(goods)
const changeSku = (skuId: string) => {
  const sku = goodsInfo.value.skus.find(item => item.id === skuId)
  if (sku) {
    goodsInfo.value.inventory = sku.inventory
    goodsInfo.value.oldPrice = sku.oldPrice
    goodsInfo.value.price = sku.price
  }
}
// 数量选择组件
const modelValue = ref(1)
const max = ref(10)
const min = ref(1)
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <div v-if="goodsInfo.categories">
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">
            {{ goodsInfo.categories[1].name }}
          </XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${goodsInfo.categories[0].id}`">
            {{ goodsInfo.categories[0].name }}
          </XtxBreadItem>
          <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
        </XtxBread>
        <!-- 商品信息 -->
        <div class="goods-info">
          <div class="media">
            <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
            <goodsSales></goodsSales>
          </div>
          <div class="spec">
            <!-- 商品名称 -->
            <GoodsName :goods="goodsInfo"></GoodsName>
            <!-- SKU商品库存 -->
            <GoodsSku :goodsInfo="goodsInfo" skuId="1369155864430120962" @changeSku="changeSku"></GoodsSku>
            <!-- 数量选择 -->
            <XtxNumBox v-model:modelValue="modelValue" :min="min" :max="max"></XtxNumBox>
            <!-- 购物车组件 -->
            <XtxButton type="primary" style="margin-top: 20px">加入购物车</XtxButton>
          </div>
        </div>
        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs">
              <!-- 商品详情 -->
              <GoodsDetail :goods="goodsInfo"></GoodsDetail>
            </div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <GoodsHot :type="1" />
            <GoodsHot :type="2" />
            <GoodsHot :type="3" />
          </div>
        </div>
      </div>
      <!-- 占位符 -->
      <div v-else>
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
        </XtxBread>
        <!-- 商品信息 -->
        <div class="goods-info"></div>
        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs"></div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
